package com.sdd.sg.controller;

import com.sdd.sg.api.UserService;
import com.sdd.sg.dto.UserAddDto;
import com.sdd.sg.request.UserAddRequest;
import com.sdd.sg.response.Response;
import com.sdd.sg.utils.DataConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * ClassName: UserController
 * Description: 用户操作controller
 * date: 2020/7/6
 *
 * @author xugaojun
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Reference
    private UserService userService;

    /**
     * 新增用户
     * @param request
     * @return
     */
    @PostMapping
    public Response saveUser(@RequestBody @Valid UserAddRequest request) {

        UserAddDto dto = DataConvertUtil.convertOne(request, UserAddDto.class);

        return Response.success (userService.saveUser(dto));
    }


    /**
     * 根据ID查询用户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Response getUserById(@PathVariable("id") Integer id) {
        log.info("============ id : {}", id);

        return Response.success(userService.getUser());
    }

}
